//
//  AddUserHeaderView.swift
//  TCMHealth
//
//  Created by 韩颖 on 2023/7/10.
//

import UIKit
import TextFieldEffects
import RxSwift
import RxCocoa

class AddUserHeaderView: UITableViewHeaderFooterView {
    @IBOutlet weak var userNameTF: TextFieldEffects!
    @IBOutlet weak var phoneTF: TextFieldEffects!
    @IBOutlet weak var maleBtn: UIButton!
    @IBOutlet weak var femaleBtn: UIButton!
    @IBOutlet weak var ageTF: TextFieldEffects!
    let disposeBug = DisposeBag()
    
    override func awakeFromNib() {
        super.awakeFromNib()

        userNameTF.rx.controlEvent(.editingDidEnd).subscribe(onNext: {
                   [weak self] (_) in
                   self?.phoneTF.becomeFirstResponder()
               }).disposed(by: disposeBug)
        
        phoneTF.rx.controlEvent(.editingDidEnd).subscribe(onNext: {
                   [weak self] (_) in
                   self?.ageTF.becomeFirstResponder()
               }).disposed(by: disposeBug)
                
 
        ageTF.rx.controlEvent(.editingDidEndOnExit).subscribe(onNext: {
                   [weak self] (_) in
                   self?.ageTF.resignFirstResponder()
               }).disposed(by: disposeBug)
        
        maleBtn.rx.tap.subscribe { _ in
            self.maleBtn.isSelected = !self.maleBtn.isSelected
            self.femaleBtn.isSelected = !self.maleBtn.isSelected
        }.disposed(by: disposeBug)
        
        femaleBtn.rx.tap.subscribe { _ in
            self.femaleBtn.isSelected = !self.femaleBtn.isSelected
            self.maleBtn.isSelected = !self.femaleBtn.isSelected
        }.disposed(by: disposeBug)

    }
    
    func getUserInfo(userInfo: UserInfo) {
        userInfo.name = userNameTF.text ?? ""
        userInfo.phone = phoneTF.text ?? ""
        userInfo.age = ageTF.text ?? ""
        userInfo.gender = self.maleBtn.isSelected ? "0" : "1"
    }
    

    
//    @IBAction func femaleClick(_ sender: Any) {
//        femaleBtn.isSelected = !femaleBtn.isSelected
//        maleBtn.isSelected = !femaleBtn.isSelected
//    }
}
